<?php

namespace App\Admin\Controllers;

use App\Models\Order;
use App\Models\SendIncome;
use App\Models\User;
use App\Models\Retail;
use App\Models\UserLog;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\MessageBag;

class SendIncomesController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '发放收益';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SendIncome);

        $grid->column('id', __('Id'));
        $grid->column('created_at', __('发放时间'));
        $grid->column('updated_at', __('修改时间'));
        $grid->column('income', __('每T发放收益'));
        $grid->column('status', __('状态'))->display(function ($status) {
            return config('conf.send_status')[$status];
        });
        $grid->column('total_income', __('总发放收益'));
        $grid->column('total_electricity_fee', __('总扣除电费'));
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableView();
        });
        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SendIncome);
        $form->select('status', __('发放类型'))->options(config('conf.send_status'));;
        $form->text('income', __('每T发放收益'));
        $form->saving(function (Form $form) {
            $date = date("Y-m-d");
            $form->model()->date = $date;
            $r = SendIncome::where(['date'=>$date])->first();
            // if($r){
            //     $error = new MessageBag([
            //         'title'   => '异常',
            //         'message' => $date.' 已经发放过收益',
            //     ]);
            //     return back()->with(compact('error'));
            // }
        });

        $form->saved(function (Form $form) {
            if($form->status == 1){
                $orders = Order::where(['status' => 1])->whereHas('user', function ($query) {
                    $query->where('status', 1);
                })->get();
                $total_income = 0;
                foreach ($orders as $order) {
                    $income = $order->computing * $form->income;
                    #扣除收益
                    (new UserLog())->change(-$income, $order->user_id, 'btc', 45, '扣除收益', $order->id);
                    Order::where('id', $order->id)->update([ 'total_income' => $order->total_income - $income]);
                    $total_income += $income;
                }
                $form->model()->total_income = $total_income;
                $form->model()->save();
            }else {
                $orders = Order::where(['status' => 1])->whereHas('user', function ($query) {
                    $query->where('status', 1);
                })->get();
                $total_income = 0;
                // $total_electricity_fee = 0;
                foreach ($orders as $order) {
                    if(date('Y-m-d H:i:s')<$order->off_time) {
                        $income = $order->computing * $form->income;
                        #发放收益
                        (new UserLog())->change($income, $order->user_id, 'btc', 42, '矿机收益', $order->id);
                        #扣除电费
                        //(new UserLog())->change(-$electricity_fee, $order->user_id, 'electricity_fee', 32, '矿机每日电费', $order->id);
                        //分销收益
                        $pid = User::where('id', $order->user_id)->first('pid');
                        if ($pid->pid != 0) {
                            $retail = Retail::where('status', '1')->orderby('computing', 'desc')->get();
                            if (!$retail->isEmpty()) {
                                $retail = $retail->toArray();
                                foreach ($retail as $v) {
                                    if ($order->computing >= $v['computing']) {
                                        (new UserLog())->change($income * $v['first'], $pid->pid, 'recommend', 53, '推荐收益', $order->id);
                                        //二级收益
                                        $spid = User::where('id', $pid->pid)->first('pid');
                                        if ($spid->pid != 0) {
                                            (new UserLog())->change($income * $v['second'], $spid->pid, 'recommend', 53, '推荐收益', $order->id);
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }else{
                        $order->update(['status'=>0]);
                    }

                    Order::where('id', $order->id)->update(['total_income' => $order->total_income + $income]);
                    $total_income += $income;
                    // $total_electricity_fee += $electricity_fee;
                }
                #保存总发放收益
                $form->model()->total_income = $total_income;
                // $form->model()->total_electricity_fee = $total_electricity_fee;
                $form->model()->save();
            }
        });
        return $form;
    }
}
